package com.epmxx.controller;

import com.epmxx.vo.RoomQuery;
import com.epmxx.vo.RoomVO;
import com.epmxx.common.Result;
import com.epmxx.domain.Hotel;
import com.epmxx.domain.Room;
import com.epmxx.service.RoomService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @Description: 酒店房间控制类
 */
@RestController
@RequestMapping("room")
public class RoomController {

    @Autowired
    private RoomService roomService;

    /**
     * 分页查询
     *
     * @param query
     * @return
     */
    @GetMapping("page.do")
    public Result page(RoomQuery query) {
        PageInfo<RoomVO> pageInfo = this.roomService.queryPage(query);
        return new Result(pageInfo);
    }

    /**
     * 查询所有酒店，渲染酒店下拉框
     *
     * @return
     */
    @GetMapping("queryHotel.do")
    public Result queryHotel() {
        List<Hotel> hotels = this.roomService.queryHotel();
        return new Result(hotels);
    }

    /**
     * 新增酒店房间信息
     *
     * @return
     */
    @PostMapping("add.do")
    public Result add(Room room) {
        this.roomService.add(room);
        return new Result();
    }

    /**
     * 根据 ID 删除酒店房间信息
     *
     * @return
     */
    @PostMapping("delete.do")
    public Result add(Integer id) {
        this.roomService.delete(id);
        return new Result();
    }

    /**
     * 修改酒店房间信息
     *
     * @return
     */
    @PostMapping("update.do")
    public Result update(Room room) {
        this.roomService.update(room);
        return new Result();
    }

    /**
     * 批量删除酒店房间信息
     *
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("deleteBatch.do")
    public Result deleteBatch(@RequestParam("id") List<Integer> id) {
        int count = this.roomService.deleteRoomBatch(id);
        if (count >= 1) {
            return new Result(200, "批量删除成功");
        }
        return new Result(500, "批量删除失败");
    }

    /**
     * 跳转到酒店信息页面
     *
     * @return
     */
    @GetMapping("toHotel.do")
    public ModelAndView toHotel() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("hotelList");
        return mv;
    }
}
